package com.example.administrator.social.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.social.R;
import com.example.administrator.social.model.ApplyInfo;
import com.example.administrator.social.model.Contact;
import com.example.administrator.social.util.HttpUtils;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by Administrator on 2018/9/17 0017.
 */

public class FriendAddRecordAdapter extends RecyclerView.Adapter<FriendAddRecordAdapter.ViewHolder> {
    Context mContext;
    List<Contact> mAddRequestList;
    LayoutInflater inflater;
    RecordCallBack mCallBack;

    public FriendAddRecordAdapter(Context mContext, List<Contact> mData) {
        this.mContext = mContext;
        this.mAddRequestList = mData;
        inflater = LayoutInflater.from(mContext);
    }

    public void updateData(List<Contact> data){
        mAddRequestList = data;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.new_friends_item_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Contact info  = mAddRequestList.get(position);
        holder.bindData(info,position);
    }

    @Override
    public int getItemCount() {
        return mAddRequestList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView headerImage;
        TextView userName,information,agreeText;
        Button agreeButton;
        public ViewHolder(View itemView) {
            super(itemView);
            headerImage = itemView.findViewById(R.id.new_friend_item_header_image);
            userName = itemView.findViewById(R.id.new_friend_item_user_name);
            information = itemView.findViewById(R.id.new_friend_item_information);
            agreeButton = itemView.findViewById(R.id.new_friend_item_agree_button);
            agreeText = itemView.findViewById(R.id.new_friend_item_agree_text);
        }

        public void bindData(Contact info, final int position) {
            if (info.getHeaderImage()==null||info.getHeaderImage().equals("")){
                Glide.with(mContext).load(HttpUtils.HTTP_URL+"img/defualt_header_icon.jpeg").into(headerImage);
            }else{
                Glide.with(mContext).load(info.getHeaderImage()).into(headerImage);
            }

            if (info.isFriend()){
                if (info.getRemarks()!=null&&!info.getRemarks().equals("")){
                    userName.setText(info.getRemarks());
                }else{
                    if(info.getNickName()==null||info.getNickName().equals("")){
                        userName.setText(info.getName());
                    }else{
                        userName.setText(info.getNickName());
                    }
                }
            }else {
                if(info.getNickName()==null||info.getNickName().equals("")){
                    userName.setText(info.getName());
                }else{
                    userName.setText(info.getNickName());
                }
            }
            information.setText(mContext.getResources().getString(R.string.phone_number)+info.getPhone());

            agreeText.setVisibility(info.isFriend() ? View.VISIBLE : View.GONE);
            agreeButton.setVisibility(info.isFriend() ? View.GONE : View.VISIBLE);
            agreeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCallBack.agreesButtonOnClick(view, position);
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCallBack.itemOnClick(view,position);
                }
            });
            itemView.setTag(info.getId());
        }
    }

    public void setCallBack(RecordCallBack callBack){
        mCallBack  = callBack;
    }

    public interface RecordCallBack{
        void agreesButtonOnClick(View view,int position);
        void itemOnClick(View view,int position);
    }
}
